package com.example.shiyan7;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v4.util.TimeUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


    private TextView tv_light,tv_pre,tv_hmdty,tv_mgntc_x,tv_mgntc_y,tv_mgntc_z;
    private SensorManager mSensroMgr;
    private Sensor sensorlux,sensorprs,sensorhmdty,sensormgntc;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv_light = findViewById(R.id.light_text);
        tv_pre=findViewById(R.id.pressure_text);
       tv_hmdty=findViewById(R.id.humidity);
        tv_mgntc_x=findViewById(R.id.mgntc_x);
        tv_mgntc_y=findViewById(R.id.mgntc_y);
        tv_mgntc_z=findViewById(R.id.mgntc_z);


        mSensroMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        sensorlux = mSensroMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
        sensorprs=mSensroMgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
        sensorhmdty=mSensroMgr.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);
        sensormgntc=mSensroMgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        mSensroMgr.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {

                 float x=event.values[0];
                float y=event.values[1];
                float z=event.values[2];
                tv_mgntc_x.setText(" :"+x);
                tv_mgntc_y.setText(" :"+y);
                tv_mgntc_z.setText(" :" +z);

            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        },sensormgntc,SensorManager.SENSOR_DELAY_NORMAL);

        mSensroMgr.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {


                float humidity = event.values[0];
                tv_hmdty.setText("humidity："+humidity);

            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        },sensorhmdty,SensorManager.SENSOR_DELAY_NORMAL);


        mSensroMgr.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {

                float pre = event.values[0];
                tv_pre.setText("pre："+pre);

            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        },sensorprs,SensorManager.SENSOR_DELAY_NORMAL);

        mSensroMgr.registerListener(new SensorEventListener() {

            @Override
            public void onSensorChanged(SensorEvent event) {

                //获取光线强度
                float lux = event.values[0];
                tv_light.setText("lux："+lux);
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO 自动生成的方法存根
            }


        }, sensorlux, SensorManager.SENSOR_DELAY_NORMAL);


    }



}


